package com.ksy.common.utils;

import java.math.BigDecimal;

public class NumUtils {
    public static double getScale2(double value) {
        try {
            BigDecimal bigDecimal = new BigDecimal(value);
            return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    public static float getScale2(float value) {
        try {
            BigDecimal bigDecimal = new BigDecimal(value);
            return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getScale2String(float value) {
        try {
            BigDecimal bigDecimal = new BigDecimal(value);
            return String.valueOf(bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return String.valueOf(value);
    }

    public static float getScale(int scale, float value) {
        if (scale < 1) {
            scale = 1;
        }
        try {
            BigDecimal bigDecimal = new BigDecimal(value);
            return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}